package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param setmealDTO
     */
    @Override
    public void add(SetmealDTO setmealDTO) {
//        添加套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.add(setmeal);
//        添加套餐菜品关系的数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish ->  {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.add(setmealDish);
        });

    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page page = setmealMapper.page(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return  pageResult;
    }

    /**
     * 根据id 查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO findByid(Long id) {
//        根据id查询套餐
        Setmeal byid = setmealMapper.findByid(id);
//        根据套餐id查询套餐下的 菜品
        List<SetmealDish> byID = setmealDishMapper.findByID(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(byid,setmealVO);
        setmealVO.setSetmealDishes(byID);
        return setmealVO;

    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
//        修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
//        删除菜品
        setmealDishMapper.deletes(setmealDTO.getId());
//        在添加菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealDishMapper.add(setmealDish);
        });

    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    public void deletes(List<Long> ids) {
//        套餐起售不能删
        ids.forEach(id-> {
            Setmeal byid = setmealMapper.findByid(id);
            if(byid.getStatus()== StatusConstant.ENABLE) {
                throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
//        套餐删除
         ids.forEach(id-> {
             setmealMapper.delete(id);
         });
//        套餐下的菜品删除
         ids.forEach(id-> {
             setmealDishMapper.deletes(id);
         });
    }

    /**
     * 起售状态
     * @param status
     */
    @Override
    public void status(Integer status,Long id) {
        Setmeal build = new Setmeal().builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(build);


    }

    /**
     * APP 查询套餐
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return  setmealMapper.list(setmeal);
    }

    /**
     * APP 端 套餐里面的菜品查询
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishBysetmealID(Long id) {
      return  setmealMapper.getDishBysetmealID(id);
    }
}
